package com.ruoyi.web.sso.service.impl;

import com.ruoyi.web.sso.common.util.AssertUtil;
import com.ruoyi.web.sso.common.util.ComUtil;
import com.ruoyi.web.sso.common.util.SignUtil;
import com.ruoyi.web.sso.domain.vo.RedisDetails;
import com.ruoyi.web.sso.service.IRedisUtilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Set;
import java.util.concurrent.TimeUnit;


/**
 * @author: Fablenas
 */
@Service
public class IRedisUtilServiceImpl implements IRedisUtilService {

    private static final String TICKET_FORMAT = "%s-%s-%s";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public String setTicket(String staffId, String platformId) {
        String key = SignUtil.mySign(staffId + platformId, "utf-8");

        String ticket = String.format(TICKET_FORMAT, key, platformId, System.currentTimeMillis());
        Long expiredTime = Long.valueOf(2592000);
        RedisDetails redisDetails = new RedisDetails();
        redisDetails.setStaffId(staffId);
        redisDetails.setPlatformId(platformId);
        try {
            //保存用户信息
            redisTemplate.opsForValue().set(ticket, redisDetails, expiredTime, TimeUnit.SECONDS);
        } catch (Exception e) {
        }
        return ticket;


    }


    @Override
    public String setValue(String key, String value) {
        Long expiredTime = Long.valueOf(2592000);
        try {
            //保存用户信息
            redisTemplate.opsForValue().set(key,value, expiredTime,TimeUnit.SECONDS);
        } catch (Exception e) {
        }
        return value;
    }

    public void deleteTicket(String ticket) {
        try {
            redisTemplate.delete(ticket);
        } catch (Exception e) {
        } finally {
            return;
        }

    }

    @Override
    public void deleteTicket(String staffId, String platformId) {
        String key = SignUtil.mySign(staffId + platformId, "utf-8");
        Set<String> set = redisTemplate.keys(key+"*");
        if (AssertUtil.isNotEmpty(set)){
            redisTemplate.delete(set);
        }
    }

    public RedisDetails getTicket(String ticket) {
        RedisDetails redisDetails = null;
        try {
            //保存用户信息
            redisDetails = (RedisDetails) redisTemplate.opsForValue().get(ticket);
        } catch (Exception e) {

        } finally {
            return redisDetails;
        }
    }

    public  String getTicket(String staffId, String platformId) {
        String key = SignUtil.mySign(staffId + platformId, "utf-8");
        Set<String> set = redisTemplate.keys(key+"*");
        return ComUtil.getString(set.toString());
    }
    /**
     * 设置键的过期时间
     * @param key 键
     * @param timeout 过期时间
     * @param unit 时间单位
     * @return 是否设置成功
     */
    @Override
    public boolean expire(String key, long timeout, TimeUnit unit) {
        try {
            return redisTemplate.expire(key, timeout, unit);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 原子递增操作
     * @param key 键
     * @return 递增后的值（若键不存在则初始化为1）
     */
    @Override
    public Long incr(String key) {
        try {
            return redisTemplate.opsForValue().increment(key, 1);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 设置键值对（支持泛型）
     * @param key 键
     * @param value 值
     * @param timeout 过期时间
     * @param unit 时间单位
     * @return 设置的值
     */
    @Override
    public <T> T set(String key, T value, long timeout, TimeUnit unit) {
        try {
            redisTemplate.opsForValue().set(key, value, timeout, unit);
            return value;
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 删除指定的 key（支持批量删除）
     * @param keys 要删除的 key 列表
     * @return 删除成功的 key 数量
     */
    @Override
    public Long delete(String... keys) {
        try {
            if (keys != null && keys.length > 0) {
                return redisTemplate.delete(java.util.Arrays.asList(keys));
            }
        } catch (Exception e) {
            // 可选：记录日志
        }
        return 0L;
    }

    /**
     * 检查 key 是否存在
     * @param key 要检查的 key
     * @return true 表示存在，false 表示不存在
     */
    @Override
    public boolean exists(String key) {
        try {
            return Boolean.TRUE.equals(redisTemplate.hasKey(key));
        } catch (Exception e) {
            return false;
        }
    }
    @Override
    public String get(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }
}
